<?php
class Pages_Plugin_Pages extends Zend_Controller_Plugin_Abstract {
	
	public function routeStartup(Zend_Controller_Request_Abstract $request){
		$front = Zend_Controller_Front::getInstance ();
		$router = $front->getRouter ();
		
		$pagesModel = new Zen_List('pages_item');
		$pages=$pagesModel->select()->setColumns(array('id','alias','aliasfull'))->setOrder('aliasfull')->getData();
		
		$routes = array();
		
		foreach ( $pages as $page ) {
			$routes['pages' . $page ['id']] = new Zend_Controller_Router_Route ( $page ['aliasfull'] . "*",
			 	array ('module' => 'pages', 'controller' => 'index', 'action' => 'index', 'alias' => $page ['aliasfull'] ) );
		}
		
		$routes['index'] = new Zend_Controller_Router_Route('/', 
			array('module' => 'pages' , 'controller' => 'index' , 'action' => 'index'));
		
		$current_routes = $router->getRoutes();
		
		foreach ($current_routes as $name=>$route) $router->removeRoute($name);
		
		$router->addRoutes($routes);
		$router->addRoutes($current_routes);
		
	}	
	
}